package com.linxz.wandnroid.appui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.animation.BaseAnimation;
import com.linxz.framework.base.BaseMVPFragment;
import com.linxz.framework.utils.ShareSDKManager;
import com.linxz.ui.GuiHelper;
import com.linxz.ui.dialogs.DialogHelper;
import com.linxz.ui.listener.WebSaveListener;
import com.linxz.ui.widgets.LinxzRefreshLayout;
import com.linxz.ui.widgets.SmartLayout;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.ProjectAdapter;
import com.linxz.wandnroid.appui.view.ProjectListView;
import com.linxz.wandnroid.pojo.WandroidProjectBean;
import com.linxz.wandnroid.presenter.ProjectListPresenter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

/**
 * @author Linxz
 * 创建日期：2020年02月29日 10:20
 * version：v4.5.4
 * 描述：
 */
public class ProjectListFragment extends BaseMVPFragment<ProjectListPresenter> implements ProjectListView {

    private SmartLayout layoutSmart;
    private LinxzRefreshLayout layoutRefresh;
    private ProjectAdapter adapter;
    private int id;

    public void setId(int id){
        this.id=id;
    }

    @Override
    protected ProjectListPresenter createPresenter() {
        return new ProjectListPresenter(this,lifecycleSubject);
    }

    @Override
    public Object setLayout() {
        return R.layout.wandroid_frag_project_list;
    }

    @Override
    public void initUI() {
        layoutSmart = findViewById(R.id.layoutSmart);
        layoutRefresh = findViewById(R.id.layoutRefresh);
        RecyclerView rvProjects = findViewById(R.id.rvProjects);
        adapter=new ProjectAdapter(mvpPresenter.getProjectBeans());
        GuiHelper.initRecycleView(activity, rvProjects,adapter,15);
        @SuppressLint("InflateParams") View headerDivier=getLayoutInflater().inflate(R.layout.ui_divider_1,null);
        adapter.addHeaderView(headerDivier);
        adapter.openLoadAnimation(new BaseAnimation() {
            @Override
            public Animator[] getAnimators(View view) {
                return new Animator[]{
                        ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                        ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
                };
            }
        });
        layoutSmart.showLoadingView();
    }

    @Override
    public void initData() {
        mvpPresenter.initCid(id);
        mvpPresenter.refreshData();
    }

    @Override
    public void setListener() {
        super.setListener();
        layoutRefresh.setOnRefreshListener(new LinxzRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mvpPresenter.refreshData();
            }
        });
        layoutRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mvpPresenter.loadMoreData();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WandroidProjectBean projectBean= (WandroidProjectBean) adapter.getData().get(position);
                WanAndroidWebViewActivity.launch(activity,projectBean.getLink());
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                final WandroidProjectBean projectBean= (WandroidProjectBean) adapter.getData().get(position);
                DialogHelper.builder()
                        .build()
                        .webViewShare(activity, new WebSaveListener() {
                            @Override
                            public void onWxFriends() {
                                ShareSDKManager.wxFriendsShare(projectBean.getLink());
                            }

                            @Override
                            public void onWxCircle() {
                                ShareSDKManager.wxZoneShare(projectBean.getLink());
                            }

                            @Override
                            public void onCollect() {
                                showToast("还未开放该功能");
                            }

                            @Override
                            public void onCopy() {
                                GuiHelper.copy(activity,projectBean.getLink());
                            }
                        });

                return false;
            }
        });
    }

    @Override
    public void onLoadSuccess() {
        layoutRefresh.finishLoadMore();
        layoutRefresh.finishRefresh();
        layoutSmart.showNormal();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadError(String message, boolean showErrorLayout) {
        layoutRefresh.finishLoadMore();
        layoutRefresh.finishRefresh();
        if (showErrorLayout){
            layoutSmart.showErrorView();
        }else{
            showToast(message);
        }
    }

    @Override
    public void onLoadEmpy(boolean showEmptyLayout) {
        layoutRefresh.finishLoadMore();
        layoutRefresh.finishRefresh();
        if (showEmptyLayout){
            layoutSmart.showEmptyView();
        }else{
            showToast("已到底线啦");
            layoutRefresh.setNoMoreData(true);
        }
    }
}
